package fr.ordre66.vlcs.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.FileEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;




public class VlcsHttpClient extends GlobalHttpClient{
	

//	private String urlBase = "http://palaismilton.appspot.com/";
	private String urlBase = "http://localhost:8080/";
	public String sendData(String path, String type, String login, String password){
		
		try {
			this.login(login, password);
            
            this.upload(path, type);
            
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "OK";
	}

	void upload(String path, String type) throws Exception{
		

	    	   String url = urlBase+"/api/upload/" + type;
	            HttpPost httppost = new HttpPost(url);

	            File file = new File(path);
System.out.println("Video size: " + file.length());
	            FileEntity reqEntity = new FileEntity(file, "binary/octet-stream");

	            httppost.setEntity(reqEntity);

	            System.out.println("executing request " + httppost.getRequestLine());
	            HttpResponse response = execute(httppost);
	            HttpEntity resEntity = response.getEntity();

	            System.out.println("----------------------------------------");
	            System.out.println(response.getStatusLine());
	            if (resEntity != null) {
	                System.out.println("Response content length: " + resEntity.getContentLength());
	                System.out.println("Chunked?: " + resEntity.isChunked());
	            }
	            EntityUtils.consume(resEntity);

	}
	
	void login(String login, String password) throws Exception{
		
        try {
            HttpPost httpost = new HttpPost(urlBase+"vlcs/login");

            List <NameValuePair> nvps = new ArrayList <NameValuePair>();
            nvps.add(new BasicNameValuePair("j_username", login));
            nvps.add(new BasicNameValuePair("j_password", password));

            httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

            HttpResponse response = execute(httpost);
            HttpEntity entity = response.getEntity();

            System.out.println("Login form get: " + response.getStatusLine());
            EntityUtils.consume(entity);

//            System.out.println("Post logon cookies:");
//            cookies = getCookieStore().getCookies();
//            if (cookies.isEmpty()) {
//                System.out.println("None");
//            } else {
//                for (int i = 0; i < cookies.size(); i++) {
//                    System.out.println("- " + cookies.get(i).toString());
//                }
//            }
            


        } finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
//            getConnectionManager().shutdown();
        }
	}
}